package cn.sportvip.controller.sport;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.sportvip.Sport.AGE;
import cn.sportvip.dto.sport.CoachDto;
import cn.sportvip.dto.sport.CoachHonor;
import cn.sportvip.dto.sport.CourseDto;
import cn.sportvip.dto.sport.GymnasiumDto;
import cn.sportvip.entity.sport.Coach;
import cn.sportvip.entity.sport.CoachBasic;
import cn.sportvip.entity.sport.Course;
import cn.sportvip.entity.sport.CourseBasic;
import cn.sportvip.entity.sport.Event;
import cn.sportvip.entity.sport.Gymnasium;
import cn.sportvip.entity.sport.GymnasiumBasic;
import cn.sportvip.entity.sport.ServicePhone;
import cn.sportvip.entity.sport.Tag;
import cn.sportvip.service.sport.CmsSportService;

@RestController
@RequestMapping("cms")
public class CmsController {
	@Autowired
	private CmsSportService cmsSportService;

	/** 增/改标签 */
	@RequestMapping("saveOrUpdateTag")
	public void saveOrUpdateTag(Integer id, String name, String icon) {
		Tag tag = new Tag();
		tag.setIcon(icon);
		tag.setName(name);
		tag.setId(id);
		cmsSportService.createTag(tag);
	}

	/** 查 标签 */
	@RequestMapping("findAllTags")
	public Iterable<Tag> findAllTags() {
		return cmsSportService.findAllTags();
	}

	/** 增/改项目 */
	@RequestMapping("saveOrUpdateEvent")
	public void saveOrUpdateEvent(Integer id, String name, String icon) {
		Event event = new Event();
		event.setIcon(icon);
		event.setName(name);
		event.setId(id);
		cmsSportService.createEvent(event);
	}

	/** 查询运动项目 */
	@RequestMapping("findAllEvents")
	public Iterable<Event> findAllEvents() {
		return cmsSportService.findAllEvents();
	}

	/** 增/改教练 */
	@RequestMapping("saveOrUpdateCoach")
	public void saveOrUpdateCoach(Integer id, String introduction,
			String resume, String name, String avatar, int age,
			@RequestParam(value = "tag", required = false) List<Integer> TagIds,
			String certificate,
			@RequestParam(value = "phone") String[] phones,
			@RequestParam(value = "year", required = false) List<String> years,
			@RequestParam(value = "honor", required = false) List<String> honors) {
		Coach coach = new Coach();
		coach.setId(id);
		coach.setIntroduction(introduction);
		coach.setResume(resume);
		
		CoachBasic basic = new CoachBasic();
		basic.setAge(age);
		basic.setAvatar(avatar);
		basic.setCertificate(certificate);
		// 荣誉
		if(years != null) {
			List<CoachHonor> honorList = new ArrayList<>(years.size());
			Iterator<String> honorsIt = honors.iterator();
			for(String year : years) {
				if(year.equals("")){
					continue;
				}
				if(honorsIt.hasNext()) {
					CoachHonor honor = new CoachHonor();
					honor.setHonor(honorsIt.next());
					honor.setYear(year);
					honorList.add(honor);
				}
			}
			basic.setHonors(honorList);
		}
		basic.setName(name);
		// 多个电话
		basic.setPhones(phones);
		basic.setZan(0);
		
		cmsSportService.createCoach(coach, basic, TagIds);
	}
	
	/** 增/改课程 */
	@RequestMapping("saveOrUpdateCourse")
	public Course saveOrUpdateCourse(Integer id, String schoolTime, String advantage, double price,
			String name, String description, @RequestParam("age") AGE[] ages, int eventId) {
		Course course = new Course();
		course.setId(id);
		course.setAdvantage(advantage);
		course.setDescription(description);
		course.setName(name);
		course.setPrice(price);
		course.setSchoolTime(schoolTime);
		
		CourseBasic basic = new CourseBasic();
		if(ages != null) {
			Set<AGE> ageSet = new HashSet<>();
			for(AGE a : ages) {
				ageSet.add(a);
			}
			basic.setAge(ageSet);
		}
		
		return cmsSportService.createCourse(course, basic, eventId);
	}
	
	/**增/改 场馆*/
	@RequestMapping("saveOrUpdateGymnasium")
	public void saveOrUpdateGymnasium(Integer id, String description, double lat, double lng, String name,
			String address, @RequestParam(value = "pic", required = false) List<String> pics, @RequestParam(value = "tag", required = false) List<Integer> tagIds) {
		Gymnasium gymnasium = new Gymnasium();
		gymnasium.setId(id);
		gymnasium.setDescription(description);
		
		GymnasiumBasic basic = new GymnasiumBasic();
		basic.setAddress(address);
		basic.setLocation(new double[]{lng, lat});
		basic.setName(name);
		basic.setPics(pics);
		
		cmsSportService.createGymnasium(gymnasium, basic, tagIds);
	}
	
	/**给课程添加教练*/
	@RequestMapping("addCoachToCourse")
	public void addCoachToCourse(int courseId, int coachId) {
		cmsSportService.addOneCourseToCoach(courseId, coachId);
	}
	
	/**给场馆添加课程*/
	@RequestMapping("addCourseToGymnasium")
	public void addCourseToGymnasium(int gymnasiumId,int courseId) {
		cmsSportService.addOneCourseToGymnasium(gymnasiumId, courseId);
	}
	
	@RequestMapping("findAllCoaches")
	public Page<CoachDto> findAllCoaches(@RequestParam(defaultValue = "1", required = false, value = "page") int pageNo,
			@RequestParam(defaultValue = "15", required = false, value = "limit") int pageSize) {
		return cmsSportService.findAllCoaches(pageNo - 1, pageSize);
	}
	
	@RequestMapping("findAllCourses")
	public Page<CourseDto> findAllCourses(@RequestParam(defaultValue = "1", required = false, value = "page") int pageNo,
			@RequestParam(defaultValue = "15", required = false, value = "limit") int pageSize) {
		return cmsSportService.findAllCourses(pageNo - 1, pageSize);
	}
	
	@RequestMapping("findAllGymnasiums")
	public Page<GymnasiumDto> findAllGymnasiums(@RequestParam(defaultValue = "1", required = false, value = "page") int pageNo,
			@RequestParam(defaultValue = "15", required = false, value = "limit") int pageSize) {
		return cmsSportService.findAllGymnasiums(pageNo - 1, pageSize);
	}
	
	@RequestMapping("findAllPhone")
	public Iterable<ServicePhone> findAllPhone(){
		return cmsSportService.findAllServicePhone();
	}
	
	@RequestMapping("saveOrUpdatePhone")
	public ServicePhone saveOrUpdatePhone(ServicePhone servicePhone){
		return cmsSportService.saveOrUpdateServicePhone(servicePhone);
	}
	
	@RequestMapping("deletePhone")
	public void deletePhone(int id){
		cmsSportService.deleteServicePhone(id);
	}
}
